package com.examinantsdk.base.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.examinantsdk.R;


public class LoadingDialog extends Dialog {
    private ImageView loadingImages;
    private TextView pl_content_txt;
    private AnimationDrawable loadingAnimation;

    @Override
    public void show() {
        if (pl_content_txt != null && pl_content_txt.getVisibility() == View.VISIBLE) {
            pl_content_txt.setVisibility(View.GONE);
        }
        super.show();
    }

    public void show(String txt) {
        if (!TextUtils.isEmpty(txt) && pl_content_txt != null) {
            pl_content_txt.setText(txt);
            pl_content_txt.setVisibility(View.VISIBLE);
        }
        super.show();
    }

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.NoTitleDialogStyle);

        setContentView(R.layout.popup_loading);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        setCanceledOnTouchOutside(false);
//        setCancelable(false);

        loadingImages = findViewById(R.id.loadingImage);
        pl_content_txt = findViewById(R.id.pl_content_txt);
        loadingAnimation = (AnimationDrawable) loadingImages.getBackground();
        loadingAnimation.start();

        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (loadingAnimation != null) {
                    loadingAnimation.start();
                }
            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (loadingAnimation != null) {
                    loadingAnimation.stop();
                }
            }
        });

    }
}
